package beans;

import java.util.List;

import dao.Knowledgepoint;
import dao.KnowledgepointDAO;

public class KnowledgePointbean {
    private KnowledgepointDAO knowledgepointDAO;

    public KnowledgePointbean() {
	knowledgepointDAO = new KnowledgepointDAO();
    }

    public boolean addKnowledgePoint(Knowledgepoint knowledgepoint) {
	return (knowledgepointDAO.save(knowledgepoint));
    }

    public boolean deleteKnowledgePoint(Knowledgepoint knowledgepoint) {
	return knowledgepointDAO.delete(knowledgepoint);
    }

    public List<Knowledgepoint> getChapterKnowledgepoints(String chapterid) {
	return knowledgepointDAO.findChapterKnowledgePoints(chapterid);
    }

    public List<Knowledgepoint> getPrelevanceKnowledges(int knowledgeid) {
	return knowledgepointDAO.findPrelevanceKnowledges(knowledgeid);
    }

    public Knowledgepoint getKnowledgepointById(int id) {
	return knowledgepointDAO.findById(id);
    }

    public String getSourceKnowledges(String chapterid) {
	List<List> lists = knowledgepointDAO.findSourceKnowledges(chapterid);
	StringBuffer buffer;
	if (lists.size() > 0) {
	    buffer = new StringBuffer();
	    for (List list : lists) {
		buffer.append(list.get(0)).append(",").append(list.get(1))
			.append(";");
	    }
	    return buffer.toString();
	} else {
	    return "null";
	}
    }
    public String getDesKnowledges(String chapterid,String knowledgeid) {
	System.out.println(knowledgeid);
	int id = Integer.parseInt(knowledgeid);
	List<List> lists = knowledgepointDAO.findDesKnowledges(chapterid,id);
	StringBuffer buffer;
	if (lists.size() > 0) {
	    buffer = new StringBuffer();
	    for (List list : lists) {
		buffer.append(list.get(0)).append(",").append(list.get(1))
			.append(";");
	    }
	    return buffer.toString();
	} else {
	    return "null";
	}
    }
}
